package game.module.battleReport.processor;

import java.util.ArrayList;
import java.util.List;

import db.proto.ProtoMessageQzmj.ARound;
import db.proto.ProtoMessageQzmj.AllAction;
import db.proto.ProtoMessageQzmj.AllPlayers;
import db.proto.ProtoMessageQzmj.AllRound;
import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.battleReport.bean.BattleReport;
import game.module.battleReport.dao.BattleReportCache;
import lion.common.ArrayUtility;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageHall.Allaction;
import lua.LuaMessageHall.Bytearray;
import lua.LuaMessageHall.C2SGetReportdetail;
import lua.LuaMessageHall.Gamesplayers;
import lua.LuaMessageHall.S2CGetReportdetail;
import lua.LuaMessageLogin.S2ErrorCode;
@MsgCodeAnn(msgcode = C2SGetReportdetail.id, accessLimit = 500)
public class GetBattleReportDetailProcessor extends  PlayingRoleMsgProcessor{

	@Override
	public void process(PlayingRole playingRole, RequestMessageRaw requestMessage) throws Exception {
		// TODO Auto-generated method stub
		C2SGetReportdetail reqMsg = C2SGetReportdetail.parse(requestMessage);
		long battleReportId = reqMsg.reportid;
		byte RoundIndex = reqMsg.index;
		BattleReport realreport =BattleReportCache.getInstance().getRealReport(battleReportId);
		if(realreport==null) {
			S2ErrorCode respMsg = new S2ErrorCode(136);
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;
		}
		AllRound allround = realreport.getAllround();
		List<ARound> rounds = allround.getRoundsList();
		if(RoundIndex>rounds.size()) {
			S2ErrorCode respMsg = new S2ErrorCode(137);//参数错误
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;
		}
		ARound around = rounds.get(RoundIndex);
		S2CGetReportdetail respMsg = new S2CGetReportdetail();
		respMsg.isSanjindao = (byte) allround.getIsSanjindao();//有无三金倒
		respMsg.danpeishuangpei = 0;
		respMsg.eatCard = (byte)allround.getEatCard();//吃牌
		respMsg.HuaCard = (byte)allround.getHuaCard(); //有无花牌
		respMsg.goldRate = (byte)allround.getGoldRate();//游金*4 || *3
		respMsg.feeCost = 	(byte)allround.getFeeCost(); //房间结算
		respMsg.countNum = (byte)allround.getRoundsCount(); //局数
		
		respMsg.goldCard = (byte) around.getGoldCard();
		
		respMsg.dice = ArrayUtility.toArray(around.getDiceList());
		
		respMsg.handcard = new Bytearray[around.getHandsCardCount()];
		respMsg.bucards = new Bytearray[around.getHandsCard2Count()];
		respMsg.huacards = new Bytearray[around.getCardHCount()];
	//	List<ByteArray> list2 = new ArrayList<LuaMessageHall.ByteArray>();
		int size = around.getHandsCardCount();
		for(int i =0;i<size;i++) {
			respMsg.handcard[i] = new Bytearray();
			respMsg.handcard[i].cards =  around.getHandsCard(i).toByteArray();
			respMsg.bucards[i] = new Bytearray();
			respMsg.bucards[i].cards = around.getHandsCard2(i).toByteArray();
			respMsg.huacards[i] = new Bytearray();
			respMsg.huacards[i].cards = around.getCardH(i).toByteArray();
		}
		
		respMsg.roundcount =(byte)around.getRoundcount();
		respMsg.aroundscore = ArrayUtility.toArray(around.getASocreList());
		int playercount = allround.getPlayersCount();
		respMsg.players = new Gamesplayers[playercount];
		AllPlayers aplay =null;
		for(int i=0;i<playercount;i++) {
			aplay = allround.getPlayers(i);
			respMsg.players[i] = new Gamesplayers();
			respMsg.players[i].player_id = aplay.getPlayerId();
			respMsg.players[i].name = aplay.getName();
			respMsg.players[i].headimg = aplay.getHeading();
			respMsg.players[i].sex = (byte)aplay.getSex();
			respMsg.players[i].jindou = aplay.getJindou();
		}
		int actionCount = around.getActionsCount();
		respMsg.allaction = new Allaction[actionCount];
		AllAction action = null;
		for(int i=0;i<actionCount;i++) {
			action = around.getActions(i);
			respMsg.allaction[i] = new Allaction();
			respMsg.allaction[i].userAction =(byte) action.getUserAction();
			respMsg.allaction[i].playerIndex = (byte)action.getPlayerIndex();
			respMsg.allaction[i].vote = (byte)action.getVote();
			respMsg.allaction[i].operatorCards = ArrayUtility.toArray(action.getOperatorCardsList());
		}
		playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {
		// TODO Auto-generated method stub
		
	}

}
